﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Eco.Handles;
using Eco.Services;
using IDE;
using PinsDescriptionEditor;
using Descriptions_PinsDescription.Model;

namespace PinsDescriptionEditor
{
    public partial class PinsDescriptionEditor  : Form
    {
        private string fileName = "";
        private IDEEcoSpace EcoSpace
        {
            get
            {
                return RootRH.EcoSpace as IDEEcoSpace;
            }
        }
        private PinsDescriptionTable pinDescriptionTable
        {
            get
            {
                return EcoSpace.Ocl.Evaluate("PinsDescriptionTable.allInstances->first").AsObject as PinsDescriptionTable;
            }
        }
        private PinsDescriptionLoader.PinsDescriptionLoader portLoader;

        public PinsDescriptionEditor(IDEEcoSpace ecoSpace)
        {
            InitializeComponent();

            RootRH.EcoSpace = ecoSpace;

            portLoader = new PinsDescriptionLoader.PinsDescriptionLoader(EcoSpace);

            new PinsDescriptionTable(ecoSpace);
        }

        #region Pins

        private void AddPinDescButton_Click(object sender, EventArgs e)
        {
            pinDescriptionTable.PinTable.Add(new PinDescription(EcoSpace));
        }

        private void DelPinDescButton_Click(object sender, EventArgs e)
        {
            if (pinsCMH.Element == null)
                return;

            (pinsCMH.Element.AsObject as PinDescription).AsIObject().Delete();
        }

        private void PinsDGV_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show("Некорректное значение поля!", "Ошибка!", 
                MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        #endregion

        #region Etalons

        private void AddEtalonButton_Click(object sender, EventArgs e)
        {
            pinDescriptionTable.Etalons.Add(new EtalonDescription(EcoSpace));
        }

        private void DelEtalonButton_Click(object sender, EventArgs e)
        {
            if (EtalonsCMH.Element == null)
                return;

            (EtalonsCMH.Element.AsObject as EtalonDescription).AsIObject().Delete();
        } 

        #endregion

        #region Menu event handlers
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Save();

            FlushEcoSpace();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var res = openPortDialog.ShowDialog();

            if (res != DialogResult.OK)
                return;

            fileName = openPortDialog.FileName;

            Save();
            FlushEcoSpace();
            portLoader.LoadPinsDescriptionTableFromFile(fileName);
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var res = savePortDialog.ShowDialog();

            if (res == DialogResult.OK)
                fileName = savePortDialog.FileName;

            portLoader.SavePortToFile(fileName);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Save();
            Close();
        } 
        #endregion

        private void FlushEcoSpace()
        {
            EcoSpace.Active = false;
            GC.Collect();
            EcoSpace.Active = true;
        }

        private void Save()
        {
            if (EcoSpace.DirtyList.HasDirtyObjects())
            {
                if (fileName != string.Empty)
                {
                    var res = savePortDialog.ShowDialog();

                    if (res == DialogResult.OK)
                        fileName = savePortDialog.FileName;
                }
                portLoader.SavePortToFile(fileName);
            }
        }
    }
}
